package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAgeCalculateBinding;
import stark.common.basic.event.EventStatProxy;

public class AgeCalculateActivity extends BaseAc<ActivityAgeCalculateBinding> {

    private Dialog mySelDateDialog;
    private WheelView wheelView1, wheelView2, wheelView3;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");

    @Override
    protected int onCreate() {
        return R.layout.activity_age_calculate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAgeCalculateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvBirthDate.setOnClickListener(this);

        //选择日期弹框
        selDateDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvBirthDate://出生日期
                mySelDateDialog.show();
                break;
            //选择日期弹框
            case R.id.tvDialogSelDateCancel://取消
                mySelDateDialog.dismiss();
                break;
            case R.id.tvDialogSelDateRight://确定
                mySelDateDialog.dismiss();
                int year = wheelView1.getCurrentItem() + 1980;
                int moon = wheelView2.getCurrentItem() + 1;
                int day = wheelView3.getCurrentItem() + 1;
                String selDate = year + "年" + (moon < 10 ? "0" + moon : moon) + "月" + (day < 10 ? "0" + day : day + "日");
                mDataBinding.tvBirthDate.setText(selDate);
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void calculate() {
        String date = mDataBinding.tvBirthDate.getText().toString();
        long startTime = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy年MM月dd日"));
        long endTime = TimeUtils.getNowMills();
        //毫秒ms
        long diff = endTime - startTime;

        long diffYears = diff / 365 / 24 / 60 / 60 / 1000;
        long diffMonths = (diff / 30 / 24 / 60 / 60 / 1000) % 12;
        long diffWeeks = diff / (7 * 24 * 60 * 60 * 1000) % 4;
        long diffDays = diff / (24 * 60 * 60 * 1000) % 7;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffSeconds = diff / (1000) % 60;
        mDataBinding.tvAge.setText(String.valueOf(diffYears));
        mDataBinding.tvYear.setText(String.valueOf(diffYears));
        mDataBinding.tvMonth.setText(String.valueOf(diffMonths));
        mDataBinding.tvWeek.setText(String.valueOf(diffWeeks));
        mDataBinding.tvDay.setText(String.valueOf(diffDays));
        mDataBinding.tvHour.setText(String.valueOf(diffHours));
        mDataBinding.tvMinute.setText(String.valueOf(diffMinutes));
        mDataBinding.tvSecond.setText(String.valueOf(diffSeconds));
    }

    //选择日期弹框
    private void selDateDialog() {
        mySelDateDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_date, null);
        mySelDateDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySelDateDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogSelDateCancel);
        TextView dialogRight = inflate.findViewById(R.id.tvDialogSelDateRight);
        wheelView1 = inflate.findViewById(R.id.wheel_view1);
        wheelView2 = inflate.findViewById(R.id.wheel_view2);
        wheelView3 = inflate.findViewById(R.id.wheel_view3);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
        wheelView1.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView1.setTextColorCenter(Color.parseColor("#0095FF"));
        wheelView1.setLineSpacingMultiplier(2.5f);
        wheelView1.setDividerType(WheelView.DividerType.WRAP);
        wheelView1.setCurrentItem(43);
        wheelView1.setItemsVisibleCount(5);
        wheelView1.setCyclic(true);
        wheelView1.setTextSize(18);
        wheelView2.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView2.setTextColorCenter(Color.parseColor("#0095FF"));
        wheelView2.setLineSpacingMultiplier(2.5f);
        wheelView2.setDividerType(WheelView.DividerType.WRAP);
        wheelView2.setCurrentItem(0);
        wheelView2.setItemsVisibleCount(5);
        wheelView2.setCyclic(true);
        wheelView2.setTextSize(18);
        wheelView3.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView3.setTextColorCenter(Color.parseColor("#0095FF"));
        wheelView3.setLineSpacingMultiplier(2.5f);
        wheelView3.setDividerType(WheelView.DividerType.WRAP);
        wheelView3.setCurrentItem(0);
        wheelView3.setItemsVisibleCount(5);
        wheelView3.setCyclic(true);
        wheelView3.setTextSize(18);
        //设置滚轮数据
        List<String> listYear = new ArrayList<>();//年 1980-2040
        for (int i = 0; i < 61; i++) {
            listYear.add(String.valueOf(1980 + i));
        }
        List<String> listMonth = new ArrayList<>();//月 12
        for (int i = 0; i < 12; i++) {
            listMonth.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listYear);
        wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMonth);
        wheelView2.setAdapter(adapter2);
        setDayDate(31);//日 默认显示1月 共31天

        //监听月份滚动选择
        wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                index = index + 1;
                if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12) {
                    //大月31天
                    setDayDate(31);
                } else if (index == 2) {
                    //2月判断是闰(29)还是平(28)
                    if (TimeUtils.isLeapYear(TimeUtils.getNowString(format), format)) {
                        setDayDate(29);
                    } else {
                        setDayDate(28);
                    }
                } else {
                    //小月30天
                    setDayDate(30);
                }
            }
        });
        //滚动到对应日期
        String date = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);//年
        int moon = Integer.parseInt(split[1]);//月
        int day = Integer.parseInt(split[2]);//日
        wheelView1.setCurrentItem(year - 1980);
        wheelView2.setCurrentItem(moon - 1);
        wheelView3.setCurrentItem(day - 1);
    }

    //设置天
    private void setDayDate(int num) {
        List<String> listDay = new ArrayList<>();//日 默认显示1月31天
        for (int i = 0; i < num; i++) {
            listDay.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter3 = new ArrayWheelAdapter(listDay);
        wheelView3.setAdapter(adapter3);
    }

}